package com.springboot.demo.io.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

/**
 * netty buffer 测试
 *
 * @author supu
 * @since 2020-11-17 10:13
 **/
public class NettyBufferTest {
    public static void main(String[] args) {
        // buffer 是堆外分配，netty 默认的就是堆外分配
//        ByteBuf byteBuf1 = ByteBufAllocator.DEFAULT.buffer(8,20);
        // heapBuffer 是堆上分配
//        ByteBuf byteBuf2 = ByteBufAllocator.DEFAULT.heapBuffer(8,20);
        // 非池化 buffer 堆外分配
//        ByteBuf byteBuf3 = UnpooledByteBufAllocator.DEFAULT.buffer(8,20);
        // 池化 buffer 堆外分配
        ByteBuf byteBuf4 = PooledByteBufAllocator.DEFAULT.buffer(8,20);

        printByteBuf(byteBuf4);

        for (int i = 0; i < 5; i++) {
            byteBuf4.writeBytes(new byte[]{1,2,3,4,5});

            printByteBuf(byteBuf4);
        }
    }

    private static void printByteBuf(ByteBuf byteBuf) {
        System.out.println("readable : " + byteBuf.isReadable());
        System.out.println("readerIndex : " + byteBuf.readerIndex());
        System.out.println("readableBytes : " + byteBuf.readableBytes());
        System.out.println("writable : " + byteBuf.isWritable());
        System.out.println("writerIndex : " + byteBuf.writerIndex());
        System.out.println("writableBytes : " + byteBuf.writableBytes());
        System.out.println("capacity : " + byteBuf.capacity());
        System.out.println("maxCapacity : " + byteBuf.maxCapacity());
        System.out.println("是否直接分配（堆外） : " + byteBuf.isDirect());

        System.out.println("***********************************");
    }


}
